var MesUnites = {
    
    init: function() {
        
        /***********************************************************************
         * Variables
         **********************************************************************/
        var TableUnites = $('table#liste_unites'),
        LinkCreate = TableUnites.find('a.create'),
        LinkUpdate = TableUnites.find('a.update'),
        LinkDelete = TableUnites.find('a.delete'),
        EditUnite = $('div#EditUnite'),
        DeleteUnite = $('div#DeleteUnite'),
        urlDelete;
        
        /***********************************************************************
         * Forms
         **********************************************************************/
        EditUnite.dialog({
            autoOpen: false,
            resizable: false,
            width: 500,
            height: 400,
            modal: true,
            buttons: {
                "Valider": function() {
                    var formData = {};
                    var UNITE_ID = EditUnite.find('input[type=hidden]#UNITE_ID').val();
                    var UNITE_LIBELLE = EditUnite.find('input[type=text]#UNITE_LIBELLE').val();
                    var UNITE_CODE = EditUnite.find('input[type=text]#UNITE_CODE').val();
                    var url = EditUnite.find('form').attr('action');
                    
                    if (UNITE_ID != '') {
                        formData.UNITE_ID = UNITE_ID;
                    }
                    formData.UNITE_LIBELLE = UNITE_LIBELLE;
                    formData.UNITE_CODE = UNITE_CODE;
                    
                    $.ajax({
                        type: "GET",
                        url: url,
                        data: formData,
                        async: false,
                        dataType: 'json',
                        success: function(unites) {
                            UpdateList(unites);
                            EditUnite.dialog('close');
                        }
                    });
                },
                Cancel: function() {
                    EditUnite.dialog('close');
                }
            },
            close: function() {
                EditUnite.html('');
            }
        });
        
        DeleteUnite.dialog({
            autoOpen: false,
            resizable: false,
            width: 'auto',
            height: 'auto',
            modal: true,
            buttons: {
                "Valider": function() {
                    $.ajax({
                        type: "GET",
                        url: urlDelete,
                        async: true,
                        dataType: 'json',
                        success: function(unites){
                            UpdateList(unites);
                            DeleteUnite.dialog('close');
                        }
                    });
                },
                Cancel: function() {
                    DeleteUnite.dialog('close');
                }
            }
        });
        
        /***********************************************************************
         * Methods on events
         **********************************************************************/
        function OnCreate(event, element) {
            event.preventDefault();
            var url = $(element).attr('href');
            EditUnite.dialog('open').load(url);
        }
        
        function OnUpdate(event, element) {
            event.preventDefault();
            var url = $(element).attr('href');
            EditUnite.dialog('open').load(url);
        }
        
        function OnDelete(event, element) {
            event.preventDefault();
            urlDelete = $(element).attr('href');
            DeleteUnite.dialog('open');
        }
    
        function UpdateList(unites) {
            var table = $('table#liste_unites'),
            tbody = table.find('tbody'),
            base_url = $('input[type=hidden]#base_url').val();
        
            tbody.empty();

            $.each(unites, function (index, unite){
                var _update = '<a href="' + base_url + 'unite/update?UNITE_ID=' + unite.UNITE_ID + '" class="btn update">Mettre à jour</a>';
                var _delete = '<a href="' + base_url + 'unite/delete?UNITE_ID=' + unite.UNITE_ID + '" class="btn delete">Supprimer</a>';

                var tr = '<tr id="' + unite.UNITE_ID + '">';
                tr = tr + '<td>' + unite.UNITE_ID + '</td>';
                tr = tr + '<td>' + unite.UNITE_LIBELLE + '</td>';
                tr = tr + '<td>' + unite.UNITE_CODE + '</td>';
                tr = tr + '<td>' + _update + '' + _delete + '</td>';
                tr = tr + '</tr>';

                table.append(tr);
            });
        
            table.find('a.create').click(function(event) { OnCreate(event, $(this)); });
            table.find('a.update').click(function(event) { OnUpdate(event, $(this)); });
            table.find('a.delete').click(function(event) { OnDelete(event, $(this)); });
        }
        
        /***********************************************************************
         * Events
         **********************************************************************/
        LinkCreate.on('click', function(event) {
            OnCreate(event, $(this));
        });
        
        LinkUpdate.on('click', function(event) {
            OnUpdate(event, $(this));
        });
        
        LinkDelete.on('click', function(event) {
            OnDelete(event, $(this));
        });
        
    }
    
};

$(function(){
    
    $(document).ready(function(){
        
        MesUnites.init();
        
    });
    
});